package com.sdp.irrigation.task;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.cache.EqRealTimeCache;
import com.sdp.irrigation.db.irrigation_eq_real_time.service.IIrrigationEqRealTimeService;
import com.sdp.irrigation.entity.IrrigationEqRealTime;
import com.sdp.irrigation.utils.BeanUtils;
import com.sdp.irrigation.utils.HttpClientUtil;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.right.cron.ScheduledOfTask;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class EqOnlineTask implements ScheduledOfTask {

	@Value("${sdp.commServerUrl}")
	private String commServerUrl;
	
	@Autowired
	private EqRealTimeCache eqRealTimeCache;
	
	@Autowired 
	private IIrrigationEqRealTimeService eqRealTimeService;
	
	@Autowired
	private HttpClientUtil httpClientUtil;
	
	@Override
	public void execute() {
		//log.debug("----------EqOnlineTask start--------");
		if (StringUtils.isEmpty(commServerUrl)) {
			log.error("commServerUrl is null!");
			return ;
		}
		try {
			String response = httpClientUtil.sendGet(commServerUrl);
			if (!StringUtils.isEmpty(response)) {
				Result<Set<String>> result = JSON.parseObject(response, new TypeReference<Result<Set<String>>>() {});
				if (result.isSuccess() && CoreConstantsEnum.系统执行成功代码.getCode().equals(result.getCode())) {
					Set<String> eqNoSet = result.getResult();
					if (eqNoSet != null) {
						List<IrrigationEqRealTime> eqRealTimeList = eqRealTimeService.list();
						List<IrrigationEqRealTime> eqRealTimeUpdateList = new ArrayList<IrrigationEqRealTime>();
						for (IrrigationEqRealTime irrigationEqRealTime : eqRealTimeList) {
							if (!StringUtils.isEmpty(irrigationEqRealTime.getOnlineStatus())
									&& IrrigationConstants.设备在线状态_在线.getCode().equals(irrigationEqRealTime.getOnlineStatus())
									&& !eqNoSet.contains(irrigationEqRealTime.getEqNo())) {
								irrigationEqRealTime.setOnlineStatus(IrrigationConstants.设备在线状态_不在线.getCode());
								eqRealTimeUpdateList.add(irrigationEqRealTime);
								//更新缓存
								//新建对象，对缓存进行部分更新
								IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
								eqRealTimeUpdate.setOnlineStatus(IrrigationConstants.设备在线状态_不在线.getCode());
								eqRealTimeCache.updateEqRealTimeCache(irrigationEqRealTime.getEqNo(), BeanUtils.redisBean2Map(eqRealTimeUpdate));
							}
						}
						
						if (eqRealTimeUpdateList.size() > 0) {
							eqRealTimeService.updateBatchById(eqRealTimeUpdateList);
						}
						
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			for (StackTraceElement stack : e.getStackTrace()) {
				log.error(stack.toString());
			}
		}
		//log.debug("----------EqOnlineTask end--------");
	}

}
